package la.bbs558.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import la.bbs558.entity.dto.SendMsgDto;
import la.bbs558.exception.MsgException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author: Shanks
 * @date: 2024/1/25 12:37
 * @desc:
 */
@Slf4j
public class SendUtils {
    //单发的URL地址为：https:// www.weiwebs.cn /msg/HttpSendSM
    private static final String SINGLE_SEND_URL = "https://www.weiwebs.cn/msg/HttpSendSM";
    //群发的URL地址为：https:// www.weiwebs.cn /msg/HttpBatchSendSM
    private static final String BATCH_SEND_URL = "https://www.weiwebs.cn/msg/HttpBatchSendSM";
    private static final String SEND_ACCOUNT = "MXT802416";
    private static final String SEND_PASSWORD = "Fy802416";

    public static JSONObject sendMsg(SendMsgDto dto) {
        log.info("发送短信dto：{}", JSON.toJSONString(dto));
        String post = HttpUtil.post(SINGLE_SEND_URL, BeanUtil.beanToMap(dto));
        log.info("发送短信结果：{}", post);
        JSONObject res = JSON.parseObject(post);
        //{"result":0,"msgid":"2150125133601352900","ts":"20240125133601"}
        if (!"0".equals(res.getString("result"))) {
            log.error("发送短信失败：{}", res.getString("result"));
            throw new MsgException("发送短信失败,msgid=" + res.getString("msgid"));
        }
        return res;
    }

    public static JSONObject sendMsg(String mobile, String msg) {
        SendMsgDto dto = createSendMsgDto(mobile, msg);
        return sendMsg(dto);
    }

    public static SendMsgDto createSendMsgDto(String mobile, String msg) {
        SendMsgDto dto = new SendMsgDto();
        dto.setAccount(SEND_ACCOUNT);
        dto.setPswd(SEND_PASSWORD);
        dto.setMobile(mobile);
        dto.setMsg(msg);
        dto.setNeedstatus("true");
        dto.setResptype("json");
        return dto;
    }

    public static void main(String[] args) {
        SendMsgDto dto = new SendMsgDto();
        dto.setAccount(SEND_ACCOUNT);
        dto.setPswd(SEND_PASSWORD);
        dto.setMobile("13678143097");
        dto.setMsg("【测试】测试电信内容第二条，验证码为123456");
        dto.setNeedstatus("true");
//        dto.setProduct("产品id");
//        dto.setExtno("扩展码");
        dto.setResptype("json");
        sendMsg(dto);
    }
}
